package com.shanzmoo.db.module.member.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shanzmoo.db.module.member.entity.MemberAddressEntity;
import com.shanzmoo.db.module.member.mapper.MemberAddressMapper;
import com.shanzmoo.db.module.member.service.IMemberAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 会员收货地址表 服务实现类
 * </p>
 *
 * @author wangyudi
 * @since 2021-04-24
 */
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddressEntity> implements IMemberAddressService {

    @Override
    public void deleteDefault(Integer memberInfoId) {
        List<MemberAddressEntity> list = baseMapper.selectList(
                new QueryWrapper<MemberAddressEntity>()
                        .eq("deleted", false)
                        .eq("member_info_id", memberInfoId)
        );

        if (CollectionUtil.isNotEmpty(list)){

            list.forEach(e-> e.setDefaultAddress(false));
            this.updateBatchById(list);
        }
    }

    @Override
    public List<MemberAddressEntity> listbyMemberId(Integer id) {
        List<MemberAddressEntity> list = baseMapper.selectList(
                new QueryWrapper<MemberAddressEntity>()
                        .eq("deleted", false)
                        .eq("member_info_id", id)
                        .orderByDesc("default_address")
        );
        return list;
    }
}
